//
//  ViewController.m
//  Nschedule
//
//  Created by 이동민 on 13. 2. 15..
//  Copyright (c) 2013년 이동민. All rights reserved.
//

#import "ViewController.h"
#import "LoginViewController.h"
#import "MonthViewController.h"
#import "TeamAddViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize carousel;
@synthesize items;
@synthesize wrap;


#pragma mark - iCasrousel mathod

- (void)setUp
{
    //set up data
    wrap = NO;
    self.items = [NSMutableArray array];
    for (int i = 0; i < 3; i++)
    {
        [items addObject:[NSNumber numberWithInt:i]];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
    {
        [self setUp];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        [self setUp];
    }
    return self;
}

- (void)dealloc
{
    //it's a good idea to set these to nil here to avoid
    //sending messages to a deallocated viewcontroller
    carousel.delegate = nil;
    carousel.dataSource = nil;
    
    [carousel release];
    [items release];
}




- (void)viewDidLoad
{
    //로그인뷰 모달로 띄우기
    UIStoryboard *storyboard = self.storyboard;
    LoginViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self presentViewController:loginController animated:YES completion:nil];
   
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
    
    carousel.type = iCarouselTypeCoverFlow2;
    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    
    
    self.carousel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)addButtonPressed:(id)sender {
    
    TeamAddViewController *teamAddViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"teamAddViewController"];
    [teamAddViewController setDelegate:self];
    [self presentViewController:teamAddViewController animated:YES completion:nil];
    
}

-(void) protocolTest:(NSString *)myString
{
    [self.items addObject:myString];
}





#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [items count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    UIButton *button = nil;
    
    //create new view if no view is available for recycling
    if (view == nil)
    {
        // image
        view = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 266.0f, 257.0f)] autorelease];
        ((UIImageView *)view).image = [UIImage imageNamed:@"TeamCal@2x.png"];
        view.contentMode = UIViewContentModeScaleToFill;

        // label
        label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = UITextAlignmentCenter;
        label.font = [label.font fontWithSize:50];
        label.tag = 1;
        
        // button
        button = [[UIButton alloc] initWithFrame:CGRectMake(200, 47, 26, 21)];
        [button setBackgroundImage:[UIImage imageNamed:@"infoButton@2x.png"] forState:UIControlStateNormal];
        
        [view addSubview:label];
        [view addSubview:button];
    }
    else
    {
        //get a reference to the label in the recycled view
        label = (UILabel *)[view viewWithTag:1];
    }
    
    //set item label
    //remember to always set any properties of your carousel item
    //views outside of the `if (view == nil) {...}` check otherwise
    //you'll get weird issues with carousel item content appearing
    //in the wrong place in the carousel
    label.text = [[items objectAtIndex:index] stringValue];
    
    return view;
}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return 0;
}

- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    
    //create new view if no view is available for recycling
    if (view == nil)
    {
        //don't do anything specific to the index within
        //this `if (view == nil) {...}` statement because the view will be
        //recycled and used with other index values later
      
        //xib로 뷰 채우기
        view = [[[NSBundle mainBundle] loadNibNamed:@"TeamCal" owner:self options:nil] lastObject];

        /*
        //ImageView로 채우기
        view = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)] autorelease];
        ((UIImageView *)view).image = [UIImage imageNamed:@"TeamCal@2x.png"];
        view.contentMode = UIViewContentModeScaleToFill;
        
        label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = UITextAlignmentCenter;
        label.font = [label.font fontWithSize:50.0f];
        label.tag = 1;
        [view addSubview:label];
         */
        
    }
    else
    {
        //get a reference to the label in the recycled view
        label = (UILabel *)[view viewWithTag:1];
    }
    
    //set item label
    //remember to always set any properties of your carousel item
    //views outside of the `if (view == nil) {...}` check otherwise
    //you'll get weird issues with carousel item content appearing
    //in the wrong place in the carousel
    label.text = (index == 0)? @"[": @"]";
    
    return view;
}

- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth);
}

- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        default:
        {
            return value;
        }
    }
}


#pragma mark -
#pragma mark Button tap event

//캘린더 클릭했을때 호출되는 함수
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    UIStoryboard *storyboard = self.storyboard;
    MonthViewController *MonthViewController = [storyboard instantiateViewControllerWithIdentifier:@"MonthView"];
    [self.navigationController pushViewController:MonthViewController animated:YES];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//로그아웃 버튼 눌렀을때 
- (IBAction)LogoutButton:(id)sender
{
    UIStoryboard *storyboard = self.storyboard;
    LoginViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self presentModalViewController:loginController animated:YES];
    
}
@end
